package cn.darxs.noquestion.controller;

import cn.darxs.noquestion.common.Response;
import cn.darxs.noquestion.entity.Admin;
import cn.darxs.noquestion.entity.Renter;
import cn.darxs.noquestion.entity.Replier;
import cn.darxs.noquestion.entity.User;
import cn.darxs.noquestion.service.AdminService;
import cn.darxs.noquestion.service.RenterService;
import cn.darxs.noquestion.service.ReplierService;
import cn.darxs.noquestion.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static cn.darxs.noquestion.common.ResponseStateCode.OPERATION_SUCCEED;
@Slf4j
@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    AdminService adminService;
    @Autowired
    RenterService renterService;
    @Autowired
    ReplierService replierService;
    @Autowired
    UserService userService;

    @PostMapping   //post请求这样写。。其他请求请参考resful风格
    public Response register(@RequestBody JSONObject params){
        String flag = params.get("flag").toString();
        String userName = params.get("userName").toString();   //前端传过来一个json格式
        String passWord = params.get("passWord").toString();
        String Information = params.get("infor").toString();
        String Name = params.get("name").toString();
        Response httpResponseEntity = new Response();
        if(flag.equals("2")){
            String RenterId = params.get("renterid").toString();
            User user = new User();
            user.setUsername(userName);
            user.setPassword(passWord);
            user.setInfo(Information);
            user.setName(Name);
            user.setRenterId(Long.parseLong(RenterId));
            userService.save(user);
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("user");
        } else if (flag.equals("1")) {
            Renter renter = new Renter();
            renter.setUsername(userName);
            renter.setPassword(passWord);
            renter.setInfo(Information);
            renter.setName(Name);
            renterService.save(renter);
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("renter");
        } else if (flag.equals("3")) {
            Replier replier = new Replier();
            replier.setUsername(userName);
            replier.setPassword(passWord);
            replier.setInfo(Information);
            replier.setName(Name);
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("replier");
        }

        return httpResponseEntity;
    }

    @GetMapping
    public Response getRenterIds(){
        Response response = new Response();
        List<Renter> list = renterService.list();
        List<Long> ids = new ArrayList<>();
        for(Renter r : list){
            ids.add(r.getId());
        }

        response.setData(ids);
        response.setCode("成功码");
        response.setMessage("");

        return response;
    }

    @GetMapping("/{status}/{uname}")
    public Response isRepeat(@PathVariable String status ,@PathVariable String uname){
        Response response = new Response();
        log.info("!!!!{}",uname);
        boolean isFind = false;
        if(status.equals("renter")){
            LambdaQueryWrapper<Renter> renterWrapper = new LambdaQueryWrapper<>();
            renterWrapper.eq(Renter::getUsername,uname);
            Renter renter = renterService.getOne(renterWrapper);
            System.out.println(renter);
            if(renter != null){
                response.setData("renterRepeat");
                isFind = true;
            }
        }else if(status.equals("user")){
            LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(User::getUsername,uname);
            User user = userService.getOne(userWrapper);
            if(user != null){
                response.setData("userRepeat");
                isFind = true;
            }
        }else{
            LambdaQueryWrapper<Replier> replierWrapper = new LambdaQueryWrapper<>();
            replierWrapper.eq(Replier::getUsername,uname);
            Replier replier = replierService.getOne(replierWrapper);
            if(replier != null){
                response.setData("replierRepeat");
                isFind = true;
            }
        }
        if(isFind == false){
            response.setData("noPeat");
            response.setCode(OPERATION_SUCCEED);
        }
        return response;
    }

}
